/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mti.javashare.dao;

import com.mti.javashare.beans.User;
import java.util.Collection;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Sinheldrin
 */
//@Repository("IPeopleDAO")
public class UserDao implements IUserDao
{
    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public User getById(int id)
    {
        Query query = entityManager.createQuery("SELECT u FROM User u WHERE u.id = :id");
        query.setParameter("id", id);
        return (User) query.getSingleResult();
    }

    @Override
    public User getByName(String name)
    {
        Query query = entityManager.createQuery("SELECT u FROM User u WHERE u.username like :name");
        query.setParameter("name", name);
        return (User) query.getSingleResult();
    }

    @Override
    public Collection<User> getAll()
    {
        Query query = entityManager.createQuery("SELECT u FROM User u");
        return (Collection<User>) query.getResultList();
    }
    
    @Override
    public void add(User user)
    {
        entityManager.persist(user);
    }

    @Override
    public void update(User user)
    {
        entityManager.merge(user);
    }

    @Override
    public void remove(User user)
    {
        entityManager.remove(user);
    }
}
